package cn.mafangui.hotel.controller.user;

import cn.mafangui.hotel.entity.Room;
import cn.mafangui.hotel.entity.RoomType;
import cn.mafangui.hotel.entity.User;
import cn.mafangui.hotel.entity.UserVipLevel;
import cn.mafangui.hotel.mapper.RoomMapper;
import cn.mafangui.hotel.mapper.UserMapper;
import cn.mafangui.hotel.mapper.UserVipLevelMapper;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.MsgType;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping(value = "/hotel/room")
@CrossOrigin("*")
public class UserRoomController {

    @Autowired
    private RoomTypeService roomTypeService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserVipLevelMapper userVipLevelMapper;

    @Resource
    private RoomMapper roomMapper;

    /**
     * 所有房型
     * @return
     */
    @RequestMapping(value = "")
    public AjaxResult getAllRoomType(String address,HttpServletRequest request){
        List<RoomType> rooms = roomTypeService.findAllType();
        Object userId = request.getSession().getAttribute("userId");
        if (userId == null){
            return ResponseTool.failed(MsgType.NOT_LOGIN);
        }
        User user = userMapper.selectByPrimaryKey((int)userId);
        double discount = 1.0;
        for (UserVipLevel userVipLevel : userVipLevelMapper.findAll()) {
            if (userVipLevel.getViplevel().equals(String.valueOf(user.getViplevel()))){
                discount = userVipLevel.getDiscount();
                break;
            }
        }
        for (RoomType room : rooms) {
            room.setDiscount(discount);
            room.setPrice(room.getPrice() * discount);
            StringBuilder builder = new StringBuilder();
            for (Room room1 : roomMapper.selectByType(room.getTypeId())) {
                builder.append(room1.getAddress());
                builder.append(";");
            }
            room.setAddress(builder.toString());
        }

        if (address != null){
            rooms.removeIf(roomType -> !roomType.getAddress().contains(address));
        }


        return ResponseTool.success(rooms);
    }

    /**
     * 查找有余量的房型
     * @return
     */
    @RequestMapping(value = "/rest")
    public AjaxResult findAllRestRoomType(){
        return ResponseTool.success(roomTypeService.findAllRestType());
    }

    /**
     * 根据id查找房型
     * @param typeId
     * @return
     */
    @RequestMapping(value = "/{typeId}")
    public AjaxResult getById(@PathVariable int typeId,HttpServletRequest request){
        Object userId = request.getSession().getAttribute("userId");
        if (userId == null){
            return ResponseTool.failed(MsgType.NOT_LOGIN);
        }
        User user = userMapper.selectByPrimaryKey((int)userId);
        double discount = 1.0;
        for (UserVipLevel userVipLevel : userVipLevelMapper.findAll()) {
            if (userVipLevel.getViplevel().equals(String.valueOf(user.getViplevel()))){
                discount = userVipLevel.getDiscount();
                break;
            }
        }

        RoomType room = roomTypeService.selectById(typeId);
        room.setDiscount(discount);
        room.setPrice(room.getPrice() * discount);

        StringBuilder builder = new StringBuilder();
        for (Room room1 : roomMapper.selectByType(room.getTypeId())) {
            builder.append(room1.getAddress());
            builder.append(";");
        }
        room.setAddress(builder.toString());


        return ResponseTool.success(room);
    }


}
